////////////////////////////// Colour Functions /////////////////////////////////////
//Convert a hex value to its decimal value - the inputed hex must be in the
// format of a hex triplet - the kind we use for HTML colours. The function
// will return an array with three values.
function hex2num(hex) {
 if(hex.charAt(0) == "#") { 
  hex = hex.slice(1);
 }
 hex = hex.toUpperCase();
 var hex_alphabets = "0123456789ABCDEF";
 var value = new Array(3);
 var k = 0;
 var int1,int2;
 for(var i=0;i<6;i+=2) {
  int1 = hex_alphabets.indexOf(hex.charAt(i));
  int2 = hex_alphabets.indexOf(hex.charAt(i+1));
  value[k] = (int1 * 16) + int2;
  k++;
 }
 return(value);
}
//Give a array with three values as the argument and the function will return
// the corresponding hex triplet.
function num2hex(triplet) {
 var hex_alphabets = "0123456789ABCDEF";
 var hex = "#";
 var int1,int2;
 for(var i=0;i<3;i++) {
  int1 = triplet[i] / 16;
  int2 = triplet[i] % 16;

  hex += hex_alphabets.charAt(int1) + hex_alphabets.charAt(int2);
 }
 return(hex);
}

time = 0.0;

function rotate(id,delay) {
  //Default values...
  if(!delay) delay = 100;

  delay = parseInt(delay);

  window.time = window.time + delay;
  start = 90.0*(window.time/1500)*(window.time/1500)*(window.time/1500)*(window.time/1500);

  var ele = document.getElementsByName(id);
  if(ele.length == 0) 
    return;

  for(var i=0;i<ele.length;i++) {
    ele[i].setAttribute("style","-webkit-transform:rotate(" + start + "deg)");
      ele[i].style.color = "#000000";  
  }

  //Stop if we have reached the target.
  if(start >= 90) 
    return;

  //Keep calling this function
  window.setTimeout("rotate('"+id+"','"+delay+"')",delay);
}

function bounce(id, y, vy) {
    y = parseFloat(y);
    vy = parseFloat(vy);

    var ele = document.getElementsByName(id);
    if(ele.length == 0) 
      return;
    
    dt = 20.0;

    dtt = dt / 1000.0;
    a = 9.82 - 20.0*y - 1.3*vy;

    vy += a*dtt;
    y += 0.5*a*dtt*dtt + dtt*vy;

    if(Math.abs(vy) < 0.1 && Math.abs(a) < 0.1) 
	return;

    //ele[0].style.top = 100*y + "px";
    ele[0].style.marginTop = 100*y + "px";

    //Keep calling this function
    window.setTimeout("bounce('"+id+"','"+y+"','"+vy+"')",dt);
}

function randomHeader()
{
    rotate("heading",20);
    return;
    var n=Math.floor(Math.random()*3)
    if(n == 0) {
	rotate("heading",20);
	return;
    }
    if(n == 1) {
	bounce("heading",0,0);
	return;
    }
    if(n == 2) {
	fadeColor("heading","#FFFFFF","#000000",4,20,'f');
	return;
    }
}

function fadeColor(id,start_hex,stop_hex,difference,delay,color_background) {
 //Default values...
 if(!difference) difference = 20;
 if(!delay) delay = 100;
 if(!start_hex) start_hex = "#FFFFFF";
 if(!stop_hex) stop_hex = "#000000";
 if(!color_background) color_background = "c";
 
 var ele = document.getElementsByName(id);
 if(ele.length == 0) return;
 var start= hex2num(start_hex);
 var stop = hex2num(stop_hex);
 
 //Make it numbers rather than strings.
 for(var i=0;i<3;i++) {
  start[i] = Number(start[i]);
  stop[i] = Number(stop[i]);
 }
 //Morph one colour to the other. If the start color is greater than the stop colour, start color will
 // be decremented till it reaches the stop color. If it is lower, it will incremented.
 for(var i=0;i<3;i++) {
  if (start[i] < stop[i]) {
   start[i] += difference;
   if(start[i] > stop[i]) start[i] = stop[i];//If we have overshot our target, make it equal - or it won't stop.
  }
  else if(start[i] > stop[i]) {
   start[i] -= difference;
   if(start[i] < stop[i]) start[i] = stop[i];
  }
 }

 //Change the color(or the background color).
 var color = "rgb("+start[0]+","+start[1]+","+start[2]+")";
 if(color_background == "b") {
  for(var i=0;i<ele.length;i++) {
    ele[i].style.backgroundColor = color;
  }
 } else if(color_background == "fb") {
  for(var i=0;i<ele.length;i++) {
    ele[i].style.color = color;
    ele[i].style.border = "1px solid " + color;
  }
 } else {
  for(var i=0;i<ele.length;i++) {
    ele[i].style.color = color;
 }
}

 //Stop if we have reached the target.
 if((start[0] == stop[0]) && (start[1] == stop[1]) && (start[2] == stop[2])) return;

 start_hex = num2hex(start);
 //Keep calling this function
 window.setTimeout("fadeColor('"+id+"','"+start_hex+"','"+stop_hex+"',"+difference+","+delay+",'"+color_background+"')",delay);
}
